/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gandulf.guilib.view.NumberPicker;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.munchlevel.data.GameInfo;
import com.munchlevel.util.ThemeManager;
import com.munchlevel.util.ThemeManager.ThemeItem;

public class MunchLevelActivity extends BaseActivity implements android.view.View.OnClickListener {

	private AdView adView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		setContentView(R.layout.main);

		adView = new AdView(this, AdSize.SMART_BANNER, MunchLevelApplication.AD_MOBS_ID);

		Button single = (Button) findViewById(R.id.btn_singleMode);
		Button party = (Button) findViewById(R.id.btn_partyMode);

		ImageButton close = (ImageButton) findViewById(R.id.ib_close);
		close.setOnClickListener(this);

		MunchLevelApplication.applyFont(single, party);

		TextView disclaimerMsg = (TextView) findViewById(R.id.txt_disclaimer_msg);
		TextView disclaimer = (TextView) findViewById(R.id.txt_disclaimer);

		disclaimerMsg.setOnClickListener(this);
		disclaimer.setOnClickListener(this);

		single.setOnClickListener(this);
		party.setOnClickListener(this);

		RelativeLayout home = (RelativeLayout) findViewById(R.id.home);

		home.addView(adView);

		// Initiate a generic request to load it with an ad
		AdRequest adRequest = new AdRequest();
		adRequest.addTestDevice(AdRequest.TEST_EMULATOR); // Emulator
		// Galaxy Tab
		adRequest.addTestDevice("42933EFAA1D6C4782D4EB5A1495748F2");
		// Galaxy Nexus
		adRequest.addTestDevice("42161DB390162C4ECECDC54F7CB3030D");

		adView.loadAd(adRequest);

	}

	@Override
	public void onDestroy() {
		if (adView != null) {
			adView.destroy();
		}
		super.onDestroy();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.munchlevel.BaseActivity#onSharedPreferenceChanged(android.content
	 * .SharedPreferences, java.lang.String)
	 */
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

		if (key == null || MyPreferenceActivity.KEY_DISCLAIMER.equals(key)) {
			boolean visible = sharedPreferences.getBoolean(MyPreferenceActivity.KEY_DISCLAIMER, true);
			if (visible) {
				findViewById(R.id.txt_disclaimer_msg).setVisibility(View.VISIBLE);
				findViewById(R.id.txt_disclaimer).setVisibility(View.VISIBLE);
				findViewById(R.id.ib_close).setVisibility(View.VISIBLE);
			} else {
				findViewById(R.id.txt_disclaimer_msg).setVisibility(View.GONE);
				findViewById(R.id.txt_disclaimer).setVisibility(View.GONE);
				findViewById(R.id.ib_close).setVisibility(View.GONE);
			}
		}

		super.onSharedPreferenceChanged(sharedPreferences, key);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean applyTheme() {

		Drawable bg = null;

		if (getScreenOrientation() == Configuration.ORIENTATION_LANDSCAPE) {
			bg = ThemeManager.getThemeDrawable(ThemeItem.BgSplashLandscape);
		} else {
			bg = ThemeManager.getThemeDrawable(ThemeItem.BgSplash);
		}

		findViewById(R.id.home).setBackgroundDrawable(bg);

		return bg != null;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_close:
			findViewById(R.id.txt_disclaimer_msg).setVisibility(View.GONE);
			findViewById(R.id.txt_disclaimer).setVisibility(View.GONE);
			v.setVisibility(View.GONE);

			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
			Editor edit = preferences.edit();
			edit.putBoolean(MyPreferenceActivity.KEY_DISCLAIMER, false);
			edit.commit();
			break;
		case R.id.txt_disclaimer:
		case R.id.txt_disclaimer_msg:
			Intent viewIntent = new Intent("android.intent.action.VIEW",
					Uri.parse("http://www.sjgames.com/general/online_policy.html"));
			startActivity(viewIntent);
			break;
		case R.id.btn_singleMode:
			startGame(1);
			break;
		case R.id.btn_partyMode:

			AlertDialog.Builder builder = new AlertDialog.Builder(this);

			final View content = getLayoutInflater().inflate(R.layout.popup_player_count, null, false);

			NumberPicker numberPicker = (NumberPicker) content.findViewById(R.id.nbtn_player);
			numberPicker.setCurrent(2);
			numberPicker.setRange(1, 10);

			builder.setView(content);

			builder.setPositiveButton(R.string.btn_play, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					NumberPicker numberPicker = (NumberPicker) content.findViewById(R.id.nbtn_player);
					int player = numberPicker.getCurrent();

					startGame(player);

				}
			});

			builder.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});

			builder.setTitle(R.string.title_player_count);
			builder.show();

			break;
		}
	}

	protected void startGame(int players) {
		if (players < 1)
			players = 1;

		GameInfo gameInfo = new GameInfo();

		gameInfo.setPlayers(players);

		if (gameInfo.getPlayer() == null)
			gameInfo.init(players, this);

		MunchLevelApplication.getInstance().setGameInfo(gameInfo);
		Intent intent = new Intent(MunchLevelActivity.this, GameActivity.class);
		startActivity(intent);
	}

}